package com.store.satring.dos;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Configurable
public class ItemReturnDetail {

    /**
     */
    @NotNull
    @ManyToOne
    private Item item;

    /**
     */
    private Integer quantity;

    /**
     */
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date returnedOn;

    /**
     */
    private String description;
    
    @Transient
    private transient String descriptionToDisplay;

    @Transient
    private transient Long invoiceId;
    
	@PersistenceContext
    transient EntityManager entityManager;

	public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("item", "quantity", "returnedOn", "description");

	public static final EntityManager entityManager() {
        EntityManager em = new ItemReturnDetail().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countItemReturnDetails() {
        return entityManager().createQuery("SELECT COUNT(o) FROM ItemReturnDetail o", Long.class).getSingleResult();
    }

	public static List<ItemReturnDetail> findAllItemReturnDetails() {
        return entityManager().createQuery("SELECT o FROM ItemReturnDetail o", ItemReturnDetail.class).getResultList();
    }

	public static List<ItemReturnDetail> findAllItemReturnDetails(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM ItemReturnDetail o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, ItemReturnDetail.class).getResultList();
    }

	public static ItemReturnDetail findItemReturnDetail(Long id) {
        if (id == null) return null;
        return entityManager().find(ItemReturnDetail.class, id);
    }

	public static List<ItemReturnDetail> findItemReturnDetailEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM ItemReturnDetail o", ItemReturnDetail.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public static List<ItemReturnDetail> findItemReturnDetailEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM ItemReturnDetail o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, ItemReturnDetail.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            ItemReturnDetail attached = ItemReturnDetail.findItemReturnDetail(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public ItemReturnDetail merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        ItemReturnDetail merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public Item getItem() {
        return this.item;
    }

	public void setItem(Item item) {
        this.item = item;
    }

	public Integer getQuantity() {
        return this.quantity;
    }

	public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

	public Date getReturnedOn() {
        return this.returnedOn;
    }

	public void setReturnedOn(Date returnedOn) {
        this.returnedOn = returnedOn;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
		if (this.description != null) {
			if (this.description.length() > 10) {
				setDescriptionToDisplay(this.description.substring(0, 10)
						+ "...");
			} else {
				setDescriptionToDisplay(this.description);
			}
		}
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public Long getInvoiceId() {
		return invoiceId;
	}

	public void setInvoiceId(Long invoiceId) {
		this.invoiceId = invoiceId;
	}

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String getDescriptionToDisplay() {
		if (this.description != null) {
			if (this.description.length() > 10) {
				setDescriptionToDisplay(this.description.substring(0, 10)
						+ "...");
			} else {
				setDescriptionToDisplay(this.description);
			}
		}
		return descriptionToDisplay;
	}

	public void setDescriptionToDisplay(String descriptionToDisplay) {
		this.descriptionToDisplay = descriptionToDisplay;
	}
	
}
